package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.CompanyDao;
import com.cn.wanxi.model.CompanyModel;
import com.cn.wanxi.service.ICompanyService;
import com.cn.wanxi.util.EnumPath;
import com.cn.wanxi.util.IOTool;
import com.cn.wanxi.util.Path;

import java.util.ArrayList;
import java.util.List;

/**
 * 逻辑服务层
 * 1.需要文件   ---创建文件
 * 2.存储数据  =---写入数据
 * 3.读取数据
 * 4.封装数据
 * 5.返回数据
 * -----------------------
 * 1.代码太多
 * 2.功能太复杂
 * 3.维护难度大
 * 4.低耦合高内聚
 * ----------------
 * 每一个事情都应该单独用一个方法
 */
public class CompanyThreeServiceImpl implements ICompanyService {
//    private static final String SPLIT = "\n";

    @Override
    public CompanyModel getCompanyModel(String path) {

//        1.创建文件
//        String path = "E:\\wanxi\\step-3\\canyin03-0809\\web";
//        String directory = "note";
//        String fileName = "company.txt";
//        File file = IOTool.createNote(path, directory, fileName);
//        2.保存数据
        List<String> data = getData();

        IOTool.writeNote(data, path, EnumPath.FILE_NAME_COMPANY.getPath());
//        3.读取数据
        String result = IOTool.readNote();
//        4.封装数据
        //        5.返回数据
        return setData(result);
    }

    private List<String> getData() {
        CompanyDao companyDao = new CompanyDao();
//        String[] data = {companyDao.getLogo(), companyDao.getPhone()companyDao.getLogo(), companyDao.getPhone(), companyDao.getLogo(), companyDao.getPhone(), companyDao.getLogo(), companyDao.getPhone(), companyDao.getLogo(), companyDao.getPhone(), companyDao.getLogo(), companyDao.getPhone(), companyDao.getLogo(), companyDao.getPhone()};
//        泛型 E叫做数据类型，为了固定list里面的数据类型 集合
//        数组和集合的区别？
        List<String> list = new ArrayList<>();
        list.add(companyDao.getLogo());
        list.add(companyDao.getPhone());
        list.add(companyDao.getNet());
        list.add(companyDao.getEmail());
        list.add(companyDao.getLinePhone());
        list.add(companyDao.getAddress());
        list.add(companyDao.getVersion());
        return list;
    }

    private CompanyModel setData(String result) {
        CompanyModel companyModel = new CompanyModel();
        String[] results = result.split(Path.SPLIT);
//        for (int i = 0; i < results.length; i++) {
//            System.out.println(results[i] + "---" + i);
//        }
//        System.out.println(result);
        companyModel.setLogo(results[0]);
        companyModel.setPhone(results[1]);
        companyModel.setNet(results[2]);
        companyModel.setAddress(results[5]);
        companyModel.setVersion(results[6]);
        companyModel.setEmail(results[3]);
        companyModel.setLinePhone(results[4]);
        return companyModel;
    }

//    private String readNote(File file) {
//        StringBuilder result = new StringBuilder();
//        try {
////            字节流InputStream 中文能不能用字节流？
////            一个中文有多少个字节？
////            String aa="中";
////            System.out.println(aa.getBytes("GBK").length);
////            InputStream inputStream=new FileInputStream(file);
////            int read = 0;
////            while (read!=-1){
////                read = inputStream.read();
////                char t= (char) read;
////                System.out.print(t);
////            }
////            字符流
//
////            Reader reader = new FileReader(file);
//            InputStream inputStream = new FileInputStream(file);
//            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
////Reader aa=new FileReader(file);
////            為什麼这里会直接转换为StringBuilder？
////            String和StringBuilder和StringBuffer的区别？
//
////            String result = "";
//            int read = 0;
////            int a=0;
////            byte b=0;
////            b+=a;//自动转型
////            b= (byte)(a + b);
//            while ((read = reader.read()) != -1) {
////                read = reader.read();
//                result.append((char) read);
////                System.out.print((char) read);
//            }
////            System.out.println(result);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return result.toString();
//    }


//
//    /**
//     * 寫入數據
//     *
//     * @param file
//     * @param data
//     */
//    private void writeNote(File file, List<String> data) {
//        try {
//            OutputStream outputStream = new FileOutputStream(file);
////            CompanyDao companyDao = new CompanyDao();
////            能不能講字符串转换为整型？
////            能不能将字符串转换为byte数组？
////            魔法值 不允许出现在正常代码里面
//
////            String[] aa = null;
//            for (int i = 0; i < data.size(); i++) {
//                outputStream.write(data.get(i).getBytes(StandardCharsets.UTF_8));
//                outputStream.write(SPLIT.getBytes(StandardCharsets.UTF_8));
//            }
////            outputStream.write(companyDao.getAddress().getBytes(StandardCharsets.UTF_8));
////            outputStream.write(split.getBytes(StandardCharsets.UTF_8));
////            outputStream.write(companyDao.getLogo().getBytes(StandardCharsets.UTF_8));
////            outputStream.write(split.getBytes(StandardCharsets.UTF_8));
////            outputStream.write(companyDao.getPhone().getBytes(StandardCharsets.UTF_8));
////            outputStream.write(split.getBytes(StandardCharsets.UTF_8));
////            outputStream.write(companyDao.getNet().getBytes(StandardCharsets.UTF_8));
////            outputStream.write(split.getBytes(StandardCharsets.UTF_8));
////            outputStream.write(companyDao.getEmail().getBytes(StandardCharsets.UTF_8));
////            outputStream.write(split.getBytes(StandardCharsets.UTF_8));
////            outputStream.write(companyDao.getVersion().getBytes(StandardCharsets.UTF_8));
//
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }

    public static void main(String[] args) {
//        String aa="02899999";
//        System.out.println(Integer.parseInt(aa));
//        new CompanyThreeServiceImpl().getCompanyModel("E:\\wanxi\\step-3\\canyin03-0809\\web");
//        System.out.println(EnumPath.FILE_NAME_COMPANY.getPath());
    }
}
